package com.users.rn.rncommon.model;

import android.os.Bundle;
import android.text.TextUtils;


import com.users.rn.rncommon.util.RNLogger;
import com.users.rn.rncommon.util.ReactConst;
import com.users.rn.rncommon.util.ReactUtils;

import java.io.File;
import java.io.Serializable;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 10:46
 */
public class PluginInfo implements Serializable {
    private String pluginId;
    private String moduleId;
    private String componentName;
    private Bundle params;
    private boolean unpack = false;//是否拆包,false 拆包；ture 不拆包
    private boolean eventFlag;//dev，test，pre环境下的热更新开关，默认关，需要到我的-设置-调试信息中打开，关直接走本地
    private Long bundleVersion;
    private String bundlePath;


    public PluginInfo(String pId, String mId, String cName, Bundle params, Boolean unpack, boolean eventFlag) {
        this.pluginId = pId;
        this.moduleId = mId;
        this.componentName = cName;
        this.params = params;
        this.unpack = unpack;
        this.eventFlag = eventFlag;
    }

    public PluginInfo(String pluginId, String componentName, Bundle params) {
        this.pluginId = pluginId;
        this.moduleId = "index";
        this.componentName = componentName;
        this.params = params;
        this.unpack = false;
        this.eventFlag = true;
    }

    /**
     * 设置bundle path和version
     *
     * @param sdPath sdcard下bundle路径
     */
    public void setBundlePathAndVersion(String sdPath, Long pluginVersion) {
        //先判断当前开关是否开启
        if (!eventFlag) {
            this.bundlePath = ReactConst.RN_ASSETS_PREFIX + getAssetsFilePath();
            this.bundleVersion = pluginVersion;
            return;
        }
        //严格校验sdcard下面是否存在bundle文件 且 sp中sdversion是否存在 且 不等于-1
        String sdCardPath = sdPath + moduleId + ".jsbundle";
        if (ReactUtils.checkSDCardFileExist(sdCardPath)) {
            this.bundlePath = sdCardPath;
        } else {
            this.bundlePath = ReactConst.RN_ASSETS_PREFIX + getAssetsFilePath();
        }
        this.bundleVersion = pluginVersion;
    }

    private boolean compareVersion(String sdVersion, String localVersion) {
        long sdV = -1;
        try {
            sdV = Long.parseLong(sdVersion);
        } catch (Exception e) {
            RNLogger.e(e.toString());
            return false;
        }
        long localV = 0;
        try {
            localV = Long.parseLong(localVersion);
        } catch (Exception e) {
            RNLogger.e(e.toString());
            return true;
        }
        return sdV >= localV;
    }

    public boolean isUnpacking() {
        //测试结果：拆包 用 非拆包形式加载 会crash 报错：can’t find variable:__d(1000/index.bundle:1)；而非拆包 用 拆包形式加载 不会挂。
        return !unpack;
    }

    public int getAnimationType() {
        int animationType = 0;
        if (params != null) {
            try {
                String type = params.getString(ReactConst.ANIMATION_TYPE);
                animationType = Integer.valueOf(type);
            } catch (Exception ignore) {
            }
        }
        return animationType;
    }

    private String getAssetsFilePath() {
        return pluginId + File.separator + moduleId + ".jsbundle";
    }

    @Override
    public String toString() {
        return "PlugModuleInfo{" +
                "pluginId='" + pluginId + '\'' +
                ", moduleId='" + moduleId + '\'' +
                ", componentName='" + componentName + '\'' +
                ", params=" + params +
                ", unpack='" + unpack + '\'' +
                ", eventFlag=" + eventFlag +
                ", bundleVersion='" + bundleVersion + '\'' +
                ", bundlePath='" + bundlePath + '\'' +
                '}';
    }

    public String getPluginId() {
        return pluginId;
    }

    public String getModuleId() {
        return moduleId;
    }

    public String getComponentName() {
        return componentName;
    }

    public Bundle getParams() {
        return params;
    }

    public boolean isEventFlag() {
        return eventFlag;
    }

    public Long getBundleVersion() {
        return bundleVersion;
    }

    public String getBundlePath() {
        return bundlePath;
    }
}
